#ifndef MAP_DOWNLOAD_ACTION_CLIENT_H
#define MAP_DOWNLOAD_ACTION_CLIENT_H

#include "rclcpp/rclcpp.hpp"
#include "rclcpp_action/rclcpp_action.hpp"
#include "rms_pkg/action/map_download.hpp"
#include <QObject>
#include <QFile>

class MapDownloadActionClient : public QObject, public rclcpp::Node
{
  Q_OBJECT

public:
  using MapDownload = rms_pkg::action::MapDownload;
  using GoalHandleMapDownload = rclcpp_action::ClientGoalHandle<MapDownload>;

  explicit MapDownloadActionClient(const rclcpp::NodeOptions & node_options = rclcpp::NodeOptions());
  
signals:
  void downloadProgress(int percentage, quint32 chunksSent, quint32 totalChunks);
  void downloadCompleted(bool success, const QString &message);
  void downloadFeedback(const QString &info);

public slots:
  void onRequestDownloadMap(const QString &robotId, const QString &mapName);

private:
  rclcpp_action::Client<MapDownload>::SharedPtr client_ptr_;
  QString currentRobotId;
  QString currentMapName;
  QFile mapFile;
  std::vector<bool> receivedChunks;  // 跟踪已接收的数据块
  quint32 totalChunks;               // 总数据块数
  
  bool is_server_ready() const;
  void send_goal(const std::string &map_name);
  
  void goal_response_callback(const GoalHandleMapDownload::SharedPtr & goal_handle);
  void feedback_callback(
    GoalHandleMapDownload::SharedPtr,
    const std::shared_ptr<const MapDownload::Feedback> feedback);
  void result_callback(const GoalHandleMapDownload::WrappedResult & result);
};

#endif